<template>
  <div class="auth-code">
    <div class="phone-box">
      <span class="title">短信验证码发送至:</span><span class="phone" v-model="phone">{{phone}}</span>
    </div>
    <div class="code-box">
      <div class="code-title">
        <input type="text" v-model="codeNum" class="code" maxlength="6" placeholder="请输入验证码" @blur="inputBlur"/>
      </div>
      <div class="code-btn">
        <timer-btn ref="timerBtn" class="mobilebtn" v-on:run="sendCode"></timer-btn>
      </div>
    </div>

    <div class="btn-box" v-show="hideBtn">
      <button type="button" class="btn" @click="mateCode">确定</button>
    </div>
  </div>
</template>

<script>
  import code from '../../components/code/code'
  import {baseUrl} from "../../common/js/env"

  export default {
    data() {
      return {
        phone: "",
        token1: "",
        token2: "",
        codeNum: "",
        docmHeight: document.documentElement.clientHeight,//默认屏幕高度
        showHeight: document.documentElement.clientHeight,//实时屏幕高度
        hideBtn: true//显示或者隐藏
      }
    },
    created() {
      this.phone = sessionStorage.getItem("reserved_phone");
    },
    mounted() {
      window.onresize = () => {
        return (() => {
          this.showHeight = document.body.clientHeight;
        })()
      };
    },
    methods: {
      checkCode() {
        let reg = /^[0-9]*$/; //验证数字
        let code = this.codeNum;
        if (code == "") {
          this.$vux.toast.text("验证码不能为空", "middle");
          return;
        } else if (!reg.test(code)) {
          this.$vux.toast.text("验证码格式不正确", "middle");
          return;
        }
        return true;
      },
      sendCode() {//获取验证码
        let phone = this.phone;
        this.$refs.timerBtn.setDisabled(true);//设置按钮不可用
        this.axios.get(baseUrl + "/login/get_token?phone=" + phone + "&i=5").then((res) => {
          let resultObj = res.data;
          if (resultObj.code == 0 && resultObj.data != null) {
            this.token1 = resultObj.data;
            this.axios.get(baseUrl + "/login/send_code?phone=" + phone + "&i=5" + "&userToken=" + this.token1).then((res) => {
              let resultObj = res.data;
              if (resultObj.code == 0) {
                this.token2 = resultObj.data;
                this.$refs.timerBtn.start(); //启动倒计时
              } else {
                this.$vux.toast.text(resultObj.msg, "middle");
                this.$refs.timerBtn.stop();
              }
            }).catch(function (error) {
              this.$vux.toast.text("网络异常", "middle");
            });
          } else {
            this.$vux.toast.text(resultObj.msg, "middle");
          }
        }).catch(function (error) {
          this.$vux.toast.text("网络异常", "middle");
        });
      },
      mateCode() {//校验验证码
        let phone = this.phone;
        let code = this.codeNum;
        if (this.checkCode()) {
          this.$vux.loading.show({
            text: '加载中',
          })
          this.axios.get(baseUrl + "/login/mate_code?phone=" + phone + "&i=5&code=" + code + "&token=" + this.token2).then((res) => {
            let mateObj = res.data;
            this.$vux.loading.hide();
            if (mateObj.code == 0) {
              this.$router.push("/withdraw_pwdupdate");
            } else {
              this.$vux.toast.text(mateObj.msg, "middle");
            }
          }).catch(() => {
            this.$vux.loading.hide();
            this.$vux.toast.text("网络异常", "middle");
          });
        }
      },
      inputBlur() {
        window.scrollTo(0, 0)
      }
    },
    watch: {
      showHeight() {
        if (this.docmHeight > this.showHeight) {
          this.hideBtn = false;
        } else {
          this.hideBtn = true;
        }
      }
    },
    components: {
      "timer-btn": code,
    }
  }
</script>

<style lang="stylus" scoped>
  .auth-code
    width: 100%
    height: 100%
    .phone-box
      display: flex
      align-items: center
      width: 92.53%
      height: 0.667rem
      margin: 0 auto
      margin-top: 0.573rem
      margin-bottom: 0.507rem
      .title
        font-size: 0.44rem
        color: rgba(102, 102, 102, 1)
      .phone
        font-size: 0.44rem
        color: #1B1B1B
    .code-box
      display: flex
      align-items: center
      width: 92.53%
      height: 1.253rem
      margin: 0 auto
      border-bottom: 1px solid #CCC
      .code-title
        font-size: 0.44rem
        color: rgba(147, 147, 147, 1)
      .code-btn
        margin-left: auto
        .mobilebtn
          background: #FFF
          opacity: 1
          color: rgba(60, 175, 255, 1)
          font-size: 0.387rem

    .btn-box
      position: fixed
      bottom: 0.48rem
      width: 100%
      text-align: center
      .btn
        width: 92.53%
        height: 1.333rem
        line-height: 1.333rem
        background: rgba(60, 175, 255, 1)
        border-radius: 0.107rem
        font-size: 0.48rem
        color: rgba(255, 255, 255, 1)
</style>



